/*
 * Copyright 2013 JiangJie
 *
 * No.01 released by JiangJie
 * ...
 */

function Chart() {

	// 配置项
	this.marginTop = 30;
	this.marginBottom = 10;
	this.marginLeft = 10;
	this.marginRight = 16;
	this.chartMarkerWidth = 5;
	this.chartDottedLineWidth = 8;
	this.chartDataPointGap = 30;
	this.chartDataPointRadius = 4;
	this.chartDataPointFillStyle = "limegreen";
	this.chartDataPointLineFillStyle = "limegreen";
	this.chartUslLineStrokeStyle = "red";
	this.chartUclLineStrokeStyle = "red";
	this.chartTargetLineStrokeStyle = "fuchsia";
	this.chartLclLineStrokeStyle = "red";
	this.chartLslLineStrokeStyle = "red";
	this.chartSpecLabelMarginRight = 4;
	this.chartSpecLabelFont = "sans-serif";
	this.chartSpecLabelFontHeight = 14;
	this.chartSpecLabelFontStyle = "normal";
	this.chartUslLabelFillStyle = "red";
	this.chartUclLabelFillStyle = "red";
	this.chartTargetLabelFillStyle = "fuchsia";
	this.chartLclLabelFillStyle = "red";
	this.chartLslLabelFillStyle = "red";
	this.chartXYAxisStokeStyle = "black";
	this.chartXYAxisLineWidth = 2;
	this.chartSpecLineWidth = 1;
	this.dataPointSelectedLineWidth = 1;
	this.dataPointSelectedLineStrokeStyle = "RoyalBlue";
	this.chartPointCommentTriangleRadius = 10;
	this.chartPointCommentTriangleFillStyle = "tomato";
	this.chartHeaderInformationFillStyle = "black";
	this.chartHeaderInformationFont = "fantasy";
	this.chartHeaderInformationFontHeight = 18;
	this.chartHeaderInformationFontStyle = "normal";
	this.yAxisLabelFillStyle = "black";
	this.yAxisLabelFont = "sans-serif";
	this.yAxisLabelFontHeight = 12;
	this.yAxisLabelFontStyle = "normal";
	this.yAxisLabelMargin = 5;

	// 非配置项
	this.data = null;
	this.upl = 0;
	this.usl = 0;
	this.ucl = 0;
	this.target = 0;
	this.lcl = 0;
	this.lsl = 0;
	this.lpl = 0;
	this.gname = null;
	this.cname = null;
	this.ctitle = null;
	this.chart_typ = null;
	this.point = new Array();
	this.canvas = null;
	this.context = null;
	this.chartCount = 0;
	this.chartIndex = 0;
	this.canvasWidth = 0;
	this.canvasHeight = 0;
	this.chartWidth = 0;
	this.chartHeight = 0;
	this.chartTopLeftX = 0;
	this.chartTopLeftY = 0;
	this.chartTopLeftForFirstChartX = 0;
	this.chartTopLeftForFirstChartY = 0;
//	this.dataPointCoordinateOneTwoThree = new Array(); marked by xinlin 
	this.dataPointCoordinateArray = new Array(); //add by xinlin
	this.clickChartSequence = 0;
	//add by lin.xin
	this.mean=0;
	this.stdDev =0;
	this.sigma  =0;
	this.sigma_ppk =0;
	this.ppk=0;
	this.pp=0;
	this.ca =0;
    this.cpk=0;
    this.cp=0;
    this.grade="A";
    
	// 初始化画板
	this.initializeCanvas = function(canvasElement, canvasWidth, canvasHeight,isShareOneCanvas) {
		this.canvas = document.getElementById(canvasElement);
		this.context = this.canvas.getContext("2d");
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		if( isShareOneCanvas == true){
			return ;
		}
		this.canvas.width = canvasWidth;
		this.canvas.height = canvasHeight;
		
		
	};

	this.initializeChart = function(data, chartCount, chartIndex) {

		// 赋值全局变量
		var context = this.context;
		this.data = data;
		this.chartCount = chartCount;
		this.chartIndex = chartIndex;
		this.upl = data.upl;
		this.usl = data.usl;
		this.ucl = data.ucl;
		this.target = data.target;
		this.lcl = data.lcl;
		this.lsl = data.lsl;
		this.lpl = data.lpl;
		this.gname = data.gname;
		this.cname = data.cname;
		this.ctitle = data.ctitle;
		this.chart_typ = data.chart_typ;

		// 计算Y轴标签的值
		var yAxisLabelValue = new Array();
		for ( var i = 0; i <= 10; i++) {
			if (i == 0) {
				yAxisLabelValue.push(this.lpl);
			} else {
				yAxisLabelValue.push(this.lpl + i * (this.upl - this.lpl) / 10);
			}
		}

		// 计算Y轴标签的最大宽度
		var YAxisLabelWidth = 0;
		var maxYAxisLabelWidth = 0;
		for ( var i = 0; i < yAxisLabelValue.length; i++) {
			YAxisLabelWidth = context.measureText(yAxisLabelValue[i]).width;
			if (YAxisLabelWidth > maxYAxisLabelWidth) {
				maxYAxisLabelWidth = YAxisLabelWidth;
			}
		}

		// 计算各个块及控制图的宽高
		this.tileWidth = this.canvasWidth;
		this.tileHeight = this.canvasHeight / chartCount;
		this.chartWidth = this.tileWidth - this.marginLeft - maxYAxisLabelWidth - this.yAxisLabelMargin - this.chartMarkerWidth - this.marginRight;
		this.chartHeight = this.tileHeight - this.marginTop - this.marginBottom;

		// 计算Y轴顶端的坐标
		if (chartIndex == 1) {
			this.chartTopLeftForFirstChartX = this.marginLeft + maxYAxisLabelWidth + this.yAxisLabelMargin + this.chartMarkerWidth;
			this.chartTopLeftForFirstChartY = this.tileHeight * (chartIndex - 1) + this.marginTop;
		}
		this.chartTopLeftX = this.marginLeft + maxYAxisLabelWidth + this.yAxisLabelMargin + this.chartMarkerWidth;
		this.chartTopLeftY = this.tileHeight * (chartIndex - 1) + this.marginTop;

		// 画控制图的背景
		context.save();
		context.translate(this.chartTopLeftX, this.chartTopLeftY);
		context.save();
		var linearGradient = context.createLinearGradient(0, 0, 0, this.canvasWidth);
		linearGradient.addColorStop(0.2, '#fdfdfd');
		linearGradient.addColorStop(0.8, '#ededed');
		context.fillStyle = linearGradient;
		context.fillRect(0, 0, this.chartWidth, this.chartHeight);

		// 画控制图的轴线（边框）
		context.restore();
		context.strokeStyle = this.chartXYAxisStokeStyle;
		context.lineWidth = this.chartXYAxisLineWidth;
		context.strokeRect(0, 0, this.chartWidth, this.chartHeight);

		// 画控制图的简要信息
		context.save();
		context.textBaseline = "bottom";
		context.font = this.chartHeaderInformationFontStyle + " " + this.chartHeaderInformationFontHeight + "px " + this.chartHeaderInformationFont;
		context.fillStyle = this.chartHeaderInformationFillStyle;
		// 先定义样式，再计算文本长度才准确
		var chartHeaderInformation = this.cname + " | " + this.ctitle + " | " + this.gname;
		var chartHeaderInformationStartX = (this.chartWidth - context.measureText(chartHeaderInformation).width) / 2;
		context.translate(chartHeaderInformationStartX, 0);
		context.fillText(chartHeaderInformation, 0, 0);
		context.restore();

		// 计算Y轴的刻度和标签
		var yAxisStep = this.chartHeight / 10;
		var markerY = 0;
		var labelX = 0;
		var labelY = 0;
		for ( var i = 0; i <= 10; i++) {
			if (i != 0 && i != 10) {

				// 画刻度
				context.beginPath();
				context.moveTo(0, markerY);
				context.lineTo(-this.chartMarkerWidth, markerY);
				context.stroke();
			}

			// 画标签
			context.font = this.yAxisLabelFontStyle + " " + this.yAxisLabelFontHeight + "px " + this.yAxisLabelFont;
			context.fillStyle = this.yAxisLabelFillStyle;
			context.textBaseline = "middle";
			labelX = -this.chartMarkerWidth - maxYAxisLabelWidth - this.yAxisLabelMargin;
			labelY = markerY;
			labelValue = yAxisLabelValue[10 - i];
			context.fillText(yAxisLabelValue[10 - i], labelX, labelY);
			markerY += yAxisStep;
		}

		// 画控制图的上规格线
		context.beginPath();
		context.moveTo(0, this.chartHeight * (this.upl - this.usl) / (this.upl - this.lpl));
		context.lineTo(this.chartWidth, this.chartHeight * (this.upl - this.usl) / (this.upl - this.lpl));
		context.lineWidth = this.chartSpecLineWidth;
		context.strokeStyle = this.chartUslLineStrokeStyle;
		context.stroke();

		// 画控制图的上规格线标签
		context.font = this.chartSpecLabelFontStyle + " " + this.chartSpecLabelFontHeight + "px " + this.chartSpecLabelFont;
		context.fillStyle = this.chartUslLabelFillStyle;
		context.textBaseline = "bottom";
		var uslLabel = "USL:" + this.usl;
		var uslLabelLength = context.measureText(uslLabel).width + this.chartSpecLabelMarginRight;
		context.fillText(uslLabel, this.chartWidth - uslLabelLength, this.chartHeight * (this.upl - this.usl) / (this.upl - this.lpl));

		// 画控制图的上控制线
		context.beginPath();
		context.moveTo(0, this.chartHeight * (this.upl - this.ucl) / (this.upl - this.lpl));
		context.lineTo(this.chartWidth, this.chartHeight * (this.upl - this.ucl) / (this.upl - this.lpl));
		context.lineWidth = this.chartSpecLineWidth;
		context.strokeStyle = this.chartUclLineStrokeStyle;
		context.stroke();

		// 画控制图的上控制线标签
		context.font = this.chartSpecLabelFontStyle + " " + this.chartSpecLabelFontHeight + "px " + this.chartSpecLabelFont;
		context.fillStyle = this.chartUclLabelFillStyle;
		context.textBaseline = "bottom";
		var uclLabel = "UCL:" + this.ucl;
		var uclLabelLength = context.measureText(uclLabel).width + this.chartSpecLabelMarginRight;
		context.fillText(uclLabel, this.chartWidth - uclLabelLength, this.chartHeight * (this.upl - this.ucl) / (this.upl - this.lpl));

		// 画控制图的目标线
		var partCount = Math.round(this.chartWidth / (2 * this.chartDottedLineWidth));
		context.lineWidth = this.chartSpecLineWidth;
		context.strokeStyle = this.chartTargetLineStrokeStyle;
		var dottedLineStartX = 0;
		for ( var i = 0; i < partCount; i++) {
			context.beginPath();
			context.moveTo(dottedLineStartX, this.chartHeight * (this.upl - this.target) / (this.upl - this.lpl));
			context.lineTo(dottedLineStartX + this.chartDottedLineWidth, this.chartHeight * (this.upl - this.target) / (this.upl - this.lpl));
			context.stroke();
			dottedLineStartX += 2 * this.chartDottedLineWidth;
		}

		// 画控制图的目标线标签
		context.font = this.chartSpecLabelFontStyle + " " + this.chartSpecLabelFontHeight + "px " + this.chartSpecLabelFont;
		context.fillStyle = this.chartTargetLabelFillStyle;
		context.textBaseline = "bottom";
		var targetLabel = "Target:" + this.target;
		var targetLabelLength = context.measureText(targetLabel).width + this.chartSpecLabelMarginRight;
		context.fillText(targetLabel, this.chartWidth - targetLabelLength, this.chartHeight * (this.upl - this.target) / (this.upl - this.lpl));

		// 画控制图的下控制线
		context.beginPath();
		context.moveTo(0, this.chartHeight * (this.upl - this.lcl) / (this.upl - this.lpl));
		context.lineTo(this.chartWidth, this.chartHeight * (this.upl - this.lcl) / (this.upl - this.lpl));
		context.lineWidth = this.chartSpecLineWidth;
		context.strokeStyle = this.chartLclLineStrokeStyle;
		context.stroke();

		// 画控制图的下控制线标签
		context.font = this.chartSpecLabelFontStyle + " " + this.chartSpecLabelFontHeight + "px " + this.chartSpecLabelFont;
		context.fillStyle = this.chartLclLabelFillStyle;
		context.textBaseline = "bottom";
		var lclLabel = "LCL:" + this.lcl;
		var lclLabelLength = context.measureText(lclLabel).width + this.chartSpecLabelMarginRight;
		context.fillText(lclLabel, this.chartWidth - lclLabelLength, this.chartHeight * (this.upl - this.lcl) / (this.upl - this.lpl));

		// 画控制图的下规格线
		context.beginPath();
		context.moveTo(0, this.chartHeight * (this.upl - this.lsl) / (this.upl - this.lpl));
		context.lineTo(this.chartWidth, this.chartHeight * (this.upl - this.lsl) / (this.upl - this.lpl));
		context.lineWidth = this.chartSpecLineWidth;
		context.strokeStyle = this.chartLslLineStrokeStyle;
		context.stroke();

		// 画控制图的下规格线标签
		context.font = this.chartSpecLabelFontStyle + " " + this.chartSpecLabelFontHeight + "px " + this.chartSpecLabelFont;
		context.fillStyle = this.chartLslLabelFillStyle;
		context.textBaseline = "bottom";
		var lslLabel = "LSL:" + this.lsl;
		var lslLabelLength = context.measureText(lslLabel).width + this.chartSpecLabelMarginRight;
		context.fillText(lslLabel, this.chartWidth - lslLabelLength, this.chartHeight * (this.upl - this.lsl) / (this.upl - this.lpl));

		context.restore();
	};

	// 画控制图的数据点
	this.drawPoint = function() {
		var context = this.context;
		context.save();
		context.translate(this.chartTopLeftX, this.chartTopLeftY);
		var point = this.data.point;

		// 画线
		var dataPointX = this.chartDataPointGap;
		for ( var i = 0; i < point.length; i++) {
			//fix by lin.xin
			 // dataPointY = this.chartHeight * (1 - (point[i].value / (this.upl - this.lpl))) ;
			 dataPointY = this.chartHeight * (1 - ( (point[i].value-this.lpl)/(this.upl - this.lpl) ) ) ;
			if (i > 0) {
				// dataPointYPrevious =  this.chartHeight * (1 - (point[i - 1].value / (this.upl - this.lpl)));
				dataPointYPrevious =  this.chartHeight * (1 - ( (point[i-1].value-this.lpl)/(this.upl - this.lpl) ) ) ;
				context.strokeStyle = this.chartDataPointLineFillStyle;
				context.beginPath();
				context.moveTo(dataPointX, dataPointY);
				context.lineTo(dataPointX - this.chartDataPointGap, dataPointYPrevious);
				context.stroke();
			}
			dataPointX += this.chartDataPointGap;
		}

		// 画点
//		var dataPointCoordinateArray = new Array();
		dataPointX = this.chartDataPointGap;
		for ( var i = 0; i < point.length; i++) {
			// context.save();
			// dataPointY = Math.round(this.chartHeight * (1 - (point[i].value / (this.upl - this.lpl))));
			dataPointY = Math.round(this.chartHeight * (1 - ( (point[i].value-this.lpl) / (this.upl - this.lpl))));
			context.fillStyle = this.chartDataPointFillStyle;
			// console.log("val:"+point[i].value + ",usl:" + this.usl +",lsl:" +this.lsl);
			//大于usl 蓝色
			if(this.usl != null && point[i].value> this.usl){
				context.fillStyle ="blue";
			}
			//小于lsl 红色
			if(this.lsl != null && point[i].value< this.lsl){
				context.fillStyle ="red";
			}
			context.beginPath();
			context.arc(dataPointX, dataPointY, this.chartDataPointRadius, 0, Math.PI * 2, false);
			var dataPointCoordinate = new Object();
			dataPointCoordinate.x = dataPointX;
			dataPointCoordinate.y = dataPointY;
			//dataPointCoordinateArray.push(dataPointCoordinate);//marked by xinlin
			this.dataPointCoordinateArray.push(dataPointCoordinate);//alert by xinlin
			context.closePath();
			context.fill();
			// context.restore();
			//marked by lin.xin
			// if (i == Math.round(point.length / 2)) {
			// 	this.drawTriangle(dataPointX, dataPointY);
			// }
			dataPointX += this.chartDataPointGap;
		}
//		this.dataPointCoordinateOneTwoThree.push(dataPointCoordinateArray);

		context.restore();
	};

	// 画三角形的函数
	this.drawTriangle = function(x, y) {
		var context = this.context;
		context.save();
		var r = this.chartPointCommentTriangleRadius;
		var context = this.context;
		context.beginPath();
		context.moveTo(x - Math.sin(Math.PI / 3) * r, y + r / 2);
		context.lineTo(x, y - r);
		context.lineTo(x + Math.sin(Math.PI / 3) * r, y + r / 2);
		context.lineTo(x - Math.sin(Math.PI / 3) * r, y + r / 2);
		context.closePath();
		context.fillStyle = this.chartPointCommentTriangleFillStyle;
		context.fill();
		context.restore();
	};

	this.activateClickEvent = function() {
		var context = this.context;
		var _this = this;
		var selectedFlag = false;

		// 很重要！！！
		$('#chart').unbind("click");
		$("#chart").click(function(event) {

			// 判断点击的是哪个控制图
			var canvasInnerCoordinateY = event.pageY - _this.canvas.offsetTop;
			switch (_this.chartCount) {
			case 1:
				_this.clickChartSequence = 1;
				break;
			case 2:
				if (canvasInnerCoordinateY < _this.tileHeight) {
					_this.clickChartSequence = 1;
				} else {
					_this.clickChartSequence = 2;
				}
				break;
			case 3:
				if (canvasInnerCoordinateY < _this.tileHeight) {
					_this.clickChartSequence = 1;
				} else if (_this.tileHeight < canvasInnerCoordinateY < _this.tileHeight * 2) {
					_this.clickChartSequence = 2;
				} else {
					_this.clickChartSequence = 3;
				}
				break;
			}

			// 控制图点选中后画一条经过数据点的竖线
			var mouseClickX = event.pageX - _this.canvas.offsetLeft - _this.chartTopLeftForFirstChartX;
			var mouseClickY = event.pageY - _this.canvas.offsetTop - _this.tileHeight * (_this.clickChartSequence - 1) - _this.chartTopLeftForFirstChartY;
			var dataPointCoordinateX = 0;
			var dataPointCoordinateY = 0;
			var dataPointRadius = _this.chartDataPointRadius;
			if (selectedFlag) {
				_this.canvas.width = _this.canvas.width;
				for ( var i = 0; i < _this.chartCount; i++) {
					_this.initializeChart(_this.data, _this.chartCount, i + 1);
					_this.drawPoint(_this.data.point);
				}
			}
			for ( var i = 0; i < _this.dataPointCoordinateOneTwoThree[_this.clickChartSequence - 1].length; i++) {
				dataPointCoordinateX = _this.dataPointCoordinateOneTwoThree[_this.clickChartSequence - 1][i].x;
				dataPointCoordinateY = _this.dataPointCoordinateOneTwoThree[_this.clickChartSequence - 1][i].y;
				if (Math.abs(mouseClickX - dataPointCoordinateX) <= dataPointRadius && Math.abs(mouseClickY - dataPointCoordinateY) <= dataPointRadius) {
					$("#inputTimeStampTxt").attr("value", _this.data.point[i].inputTimestamp);
					$("#pointValueTxt").attr("value", _this.data.point[i].value);
					$("#prdInfoSeqTxt").attr("value", _this.data.point[i].glassId);
					$("#pToolIDTxt").attr("value", _this.data.point[i].processEqptId);
					$("#mToolIDTxt").attr("value", _this.data.point[i].measurementEqptId);
					$("#commentTxt").attr("value", _this.data.point[i].comment);
					for ( var j = 0; j < _this.chartCount; j++) {
						context.save();
						context.translate(_this.chartTopLeftForFirstChartX, _this.chartTopLeftForFirstChartY + _this.tileHeight * j);
						context.lineWidth = _this.dataPointSelectedLineWidth;
						context.beginPath();
						context.moveTo(dataPointCoordinateX, 0);
						context.lineTo(dataPointCoordinateX, _this.chartHeight);
						context.strokeStyle = _this.dataPointSelectedLineStrokeStyle;
						context.lineWidth = _this.dataPointSelectedLineWidth;
						context.stroke();
						context.restore();
						selectedFlag = true;
					}
					break;
				}
			}
		});
	};
	this.caclCPk = function(){
		this.mean = sum/pointCnt;
		this.stdDev =0;
		this.sigma  =0;
		this.ppk=0;
		this.pp=0;
		this.ca =0;
	    this.cpk=0;
	    this.cp=0;
	    this.grade="A";

		var point = this.data.point;
		if(point.length<2){
			showErrorDialog("003","数据小于两行,不能计算CPK");
			return false;	
		}
		//计算mean
		var sum = 0;
		var pointCnt = point.length ;
		for(var i=0;i<pointCnt;i++){
			sum = sum + point[i].value;
		}
		this.mean = sum/pointCnt;
		//计算sigma
		// Math.pow() -- 返回底数的指定次幂
		// Math.sqrt() -- 返回数字的平方根
		var tmpSquSum=0;
		for(var i=0;i<pointCnt;i++){
			tmpSquSum = tmpSquSum + Math.pow( (point[i].value-this.mean) , 2);
		}
		this.sigma = Math.sqrt( tmpSquSum /(pointCnt-1) );
		//计算ca 
		if(this.usl != null && this.lsl !=null ){
			this.ca = (this.mean - this.target)/(0.5* (this.usl-this.lsl));	
		}else{
			this.ca =null;
		}
		//计算cp 
		if(this.usl!=null && this.lsl==null){
			this.cp = (this.usl-this.mean)/(3*this.sigma);
		}else if(this.usl==null && this.lsl!=null){
			this.cp = (this.mean-this.lsl)/(3*this.sigma);
		}else if(this.usl!=null && this.lsl!=null){
			this.cp = (this.usl-this.lsl)/(6*this.sigma);
		}else{
			this.cp=null;
		}
		//计算cpk
		// Math.abs() -- 返回数字的绝对值
		if(this.ca ==null){
			this.cpk = this.cp;
		}else if(this.cp==null){
			this.cpk = null;
		}else{
			this.cpk = (1-Math.abs(this.ca))*this.cp;
		}
		//计算 sigma_ppk
		if(this.chart_typ=="01"){
			var tmpSumSum_ppk=0;
			var pointCnt = this.data.point.length;
			var sampleSize = this.data.point[0].rawData.length;
			for(var i=0;i<this.data.point[i].rawData.length;i++){
				var tmpSum_ppk=0;
				for(var j=0;j<this.data.point[i].rawData.length;j++){
					var rawValue =  this.data.point[i].rawData[j].prd_raw_value;
					tmpSum_ppk = tmpSum_ppk + Math.pow(( rawValue - this.mean),2);
				}
				tmpSumSum_ppk = tmpSumSum_ppk + tmpSum_ppk;
			}
			this.sigma_ppk  = Math.sqrt(tmpSumSum_ppk/(pointCnt*sampleSize-1));
				
			//计算 pp
			if( this.usl !=null && this.lsl !=null ){
				this.pp = (this.usl-this.lsl)/(this.sigma_ppk*6);
			}else if(this.usl!=null&& this.lsl==null){
				this.pp = (this.usl - this.mean)/(this.sigma_ppk*3);
			}else if(this.usl ==null && this.lsl !=null ){
				this.pp = (this.mean-this.lsl)/(this.sigma_ppk*3);
			}
			//计算 ppk
			if(this.usl!=null && this.lsl!=null){
				this.ppk = (1-Math.abs(this.ca) ) * this.pp;
			}else{
				this.ppk = this.pp;
			}
			
		}else{
			this.sigma_ppk = this.sigma ;
		}
		
		
	
		
		//
		//计算grade 
		if(this.cpk>=2){
			this.grade = "A";
		}else if(this.cpk>=1.66){
			this.grade = "B";
		}else if(this.cpk>=1.33){
			this.grade = "C";
		}else if(this.cpk>=1){
			this.grade = "D";
		}else{
			this.grade ="E";
		}
	}
}

//add by xinlin 
var Page = function(){
	this.chartList = new Array();
	this.canvas ={};
	this.clickChartSequence=1;
	this.chartCount = 0;
	this.activeChart ={};
	this.addChart = function(chart){
		this.chartList.push(chart);
		this.canvas=chart.canvas;
		this.chartCount++;
	}
	
	this.activateClickEvent = function() {
		var _this = this;
		
		var selectedFlag = false;

		// 很重要！！！
		$("#chart").unbind("click");
		$("#chart").click(function(event) {
			// 判断点击的是哪个控制图
			var canvasInnerCoordinateY = event.pageY - _this.canvas.offsetTop;
			switch (_this.chartCount) {
			case 1:
				_this.clickChartSequence = 1;
				_this.activeChart = _this.chartList[0];
				break;
			case 2:
				if (canvasInnerCoordinateY < _this.chartList[0].tileHeight) {
					_this.clickChartSequence = 1;
					_this.activeChart = _this.chartList[0];
				} else {
					_this.clickChartSequence = 2;
					_this.activeChart = _this.chartList[1];
				}
				break;
			case 3:
				if (canvasInnerCoordinateY < _this.tileHeight) {
					_this.clickChartSequence = 1
					_this.activeChart = _this.chartList[0];;
				} else if (_this.tileHeight < canvasInnerCoordinateY < _this.tileHeight * 2) {
					_this.clickChartSequence = 2;
					_this.activeChart = _this.chartList[1];
				} else {
					_this.clickChartSequence = 3;
					_this.activeChart = _this.chartList[2];
				}
				break;
			}
			var context = _this.activeChart.context;

			// 控制图点选中后画一条经过数据点的竖线
			var bugGap = 30*(_this.clickChartSequence-1);
			var mouseClickX = event.pageX - _this.canvas.offsetLeft - _this.activeChart.chartTopLeftForFirstChartX-bugGap;
			var mouseClickY = event.pageY - _this.canvas.offsetTop - _this.activeChart.tileHeight * (_this.clickChartSequence - 1) - _this.activeChart.chartTopLeftForFirstChartY-bugGap;
			
			var dataPointCoordinateX = 0;
			var dataPointCoordinateY = 0;
			var dataPointRadius = _this.activeChart.chartDataPointRadius;
			for ( var i = 0; i < _this.activeChart.dataPointCoordinateArray.length; i++) {
				dataPointCoordinateX = _this.activeChart.dataPointCoordinateArray[i].x;
				dataPointCoordinateY = _this.activeChart.dataPointCoordinateArray[i].y;
				if (Math.abs(mouseClickX - dataPointCoordinateX) <= dataPointRadius && Math.abs(mouseClickY - dataPointCoordinateY) <= dataPointRadius) {
					// alert(_this.activeChart.data.point[i].value);
					$("#inputTimeStampTxt").attr("value", _this.activeChart.data.point[i].inputTimestamp);
					$("#pointValueTxt").attr("value", _this.activeChart.data.point[i].value);
					$("#prdInfoSeqTxt").attr("value", _this.activeChart.data.point[i].glassId);
					$("#pToolIDTxt").attr("value", _this.activeChart.data.point[i].processEqptId);
					$("#mToolIDTxt").attr("value", _this.activeChart.data.point[i].measurementEqptId);
					$("#commentTxt").attr("value", _this.activeChart.data.point[i].comment);
//					for ( var j = 0; j < _this.chartCount; j++) {
//						context.save();
//						context.translate(_this.activeChart.chartTopLeftForFirstChartX, _this.activeChart.chartTopLeftForFirstChartY + _this.activeChart.tileHeight * j);
//						context.lineWidth = _this.activeChart.dataPointSelectedLineWidth;
//						context.beginPath();
//						context.moveTo(dataPointCoordinateX, 0);
//						context.lineTo(dataPointCoordinateX, _this.chartHeight);
//						context.strokeStyle = _this.activeChart.dataPointSelectedLineStrokeStyle;
//						context.lineWidth = _this.activeChart.dataPointSelectedLineWidth;
//						context.stroke();
//						context.restore();
//						selectedFlag = true;
//					}
					break;
				}
			}
		});
	};
	
}
